package mya.mes.mdc.logic.alarm.tag;

import mya.common.jpa.context.ServiceContext;
import mya.common.util.Utils;
import mya.mes.mdc.logic.tag2.ExcelTagAlarmDef;
import mya.mes.mdc.model.alarm.MdcAlarmType;
import mya.mes.mdc.share.api.MdcModule;
import mya.mes.mdc.share.vo.MdcOptions;

import java.util.ArrayList;
import java.util.List;
import java.util.TreeMap;

public class AlarmTypeUpdater2 {

    public void update(ServiceContext context, List<ExcelTagAlarmDef> tagAlarmDefList) {

        MdcOptions mdcOptions = MdcModule.getInstance().getMdcOptions();

        List<MdcAlarmType> dbList = loadAll(context);

        if (Utils.isNullOrEmpty(tagAlarmDefList)) {

            if (Utils.isNullOrEmpty(dbList)) {
                return;
            }

            for (MdcAlarmType alarmType : dbList) {
                alarmType.setActive(Boolean.FALSE);
            }

            return;
        }

        TreeMap<Long, MdcAlarmType> dbMap = new TreeMap<>();

        for (MdcAlarmType dbAlarmType : dbList) {
            dbMap.put(dbAlarmType.getObjectId(), dbAlarmType);
        }

        List<MdcAlarmType> list = build(mdcOptions, tagAlarmDefList);
        try {
            for (MdcAlarmType alarmType : list) {
                MdcAlarmType dbAlarmType = dbMap.get(alarmType.getObjectId());

                if (dbAlarmType != null) {
                    //更新
                    copySrcToDest(alarmType, dbAlarmType);

                    updateWeixinInfo(dbAlarmType, mdcOptions);

                    dbMap.remove(alarmType.getObjectId());
                } else {
                    //新增
                    context.em.persist(alarmType);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        dbMap.values().forEach(dbAlarmType -> {
            dbAlarmType.setActive(Boolean.FALSE);
        });

    }

    private void copySrcToDest(MdcAlarmType src, MdcAlarmType dest) {

//        dest.setTimeWaitToPerson(src.getTimeWaitToPerson());
//        dest.setTimeWaitToManager(src.getTimeWaitToManager());
//
//        dest.setNeedSendToPerson(src.getNeedSendToPerson());
//        dest.setNeedSendToManager(src.getNeedSendToManager());
//
//        dest.setWxPersonTag(src.getWxPersonTag());
//
//        dest.setWxManagerTag(src.getWxManagerTag());

        dest.setEnUs(src.getEnUs());

        dest.setZhCN(src.getZhCN());
    }


    private List<MdcAlarmType> loadAll(ServiceContext context) {
        List<MdcAlarmType> list =
                context.em.createQuery("select o from MdcAlarmType o ", MdcAlarmType.class).getResultList();

        return list;
    }

    @SuppressWarnings("all")
    private List<MdcAlarmType> build(MdcOptions mdcOptions, List<ExcelTagAlarmDef> tagAlarmDefList) {
        List<MdcAlarmType> list = new ArrayList<>(tagAlarmDefList.size());


        String rdbTag = null;
        String prefix = null;
        Integer tagIndex = null;
        for (ExcelTagAlarmDef tag : tagAlarmDefList) {

            if (tag.rdbTag != null && !tag.rdbTag.isEmpty()) {
                if (!tag.rdbTag.equals(rdbTag)) {
                    rdbTag = tag.rdbTag;

                    prefix = rdbTag.substring(0, 2).toUpperCase();

                    tagIndex = Integer.parseInt(rdbTag.substring(2));
                }
            }

            if (rdbTag == null) {
                continue;
            }
            MdcAlarmType alarmType = new MdcAlarmType();
            alarmType.setActive(Boolean.TRUE);


            alarmType.setMdcTag(rdbTag);
            alarmType.setTagPrefix(prefix);
            alarmType.setTagIndex(tagIndex);

            alarmType.setBitIndex(tag.bit);


            alarmType.setEnUs(tag.enUs);
            alarmType.setZhCN(tag.zhCN);


            updateWeixinInfo(alarmType, mdcOptions);

            long objectId = 1_0000_00L + tagIndex * 100 + tag.bit;
            alarmType.setObjectId(objectId);

            list.add(alarmType);
        }

        return list;
    }


    private void updateWeixinInfo(MdcAlarmType alarmType, MdcOptions mdcOptions) {

        if (alarmType.getNeedSendToPerson() == null)
            alarmType.setNeedSendToPerson(Boolean.TRUE);

        if (alarmType.getNeedSendToManager() == null)
            alarmType.setNeedSendToManager(Boolean.FALSE);

        if (alarmType.getTimeWaitToPerson() == null)
            alarmType.setTimeWaitToPerson(mdcOptions.TIME_WAIT_TO_PERSON_DEFAULT);

        if (alarmType.getTimeWaitToManager() == null)
            alarmType.setTimeWaitToManager(mdcOptions.TIME_WAIT_TO_MANAGER_DEFAULT);
    }


}
